'''
Created on 23/09/2012

@author: Christian
'''

from MySQLDB import MySQLDB
from RepositoryHG import RepositoryHG
from RepositoryGIT import RepositoryGIT
    
class VC2DB(object):
    
    def __init__(self, parm):
        self.extractParms(parm)
        self.createRepository()
    
    def extractParms(self, parm):
        self._repotype = parm[1].upper()
        self._pathurl = parm[2]
        self._projectname = parm[3]
        if len(parm) > 4:
            self._step = int(parm[4])
        else:
            self._step = 1000
        if len(parm) > 5:
            self._startingrev = int(parm[5])
        else:
            self._startingrev = 0
            
    def createRepository(self):
        if self._repotype == 'HG':
            self._repo = RepositoryHG(self._pathurl, self._projectname)
        elif self._repotype == 'GIT':
            self._repo = RepositoryGIT(self._pathurl, self._projectname)
    
    def establishConnections(self):
        self._db = MySQLDB()
        self._db.connect()
        self._repo.connect()
        
    def convert(self):
        if self._startingrev == 0:
            repoid = self._db.registerProject(self._projectname, self._pathurl, self._repo.type)
        else:
            repoid = self._db.getProjectIdByName(self._projectname)
            
        firstid = self._db.getLastInsertedId()
        fishingrev = self._repo.getSize()
        counter = 0
    
        while self._startingrev <= fishingrev:
            changesandfiles = self._repo.getChangesAndFiles((firstid + (counter * self._step)), repoid, self._startingrev, self._startingrev + self._step)
            self._db.saveChangesAndFiles(changesandfiles[0], changesandfiles[1])
            self._startingrev += self._step
            counter += 1
        
    
                 
       
    
           
    
    
    
    
    
    
   
    
    

    

